extends Node

# 0 fullscreennoborder 1 fullscreen 2 windowed
var window_mode: int = 0
var player_data: PlayerData
var current_level_data: LevelData = null
var levels_data: Array[LevelData] = []

# start data
var start_player_data: PlayerData = preload("res://resources/player_data/player_start_data.tres").duplicate(true)
var start_level_data: LevelData = preload("res://resources/level/start_level_data.tres").duplicate(true)

enum GameResult{
	Fail, Success
}
var game_end_result: GameResult = GameResult.Fail
var is_new_game: bool = true


func add_item(item_config: ItemBaseConfig):
	if not item_config.stackable:
		var new_item = create_new_item(item_config)
		player_data.items.append(new_item)
	else:
		var target_item = find_item_by_name(item_config.name) as ItemData
		if not target_item:
			var new_item = create_new_item(item_config)
			player_data.items.append(new_item)
		else:
			target_item.item_count += 1


func reduce_item_by_name(name: String):
	var target_item = find_item_by_name(name)
	if not target_item: return
	
	if target_item.item_config.stackable:
		target_item.item_count -= 1
		if target_item.item_count <= 0:
			player_data.items.erase(target_item)
	else:
		player_data.items.erase(target_item)
		

func reduce_item(item_data: ItemData):
	item_data.item_count -= 1
	if item_data.item_count <= 0:
		player_data.items.erase(item_data)
	

func remove_item(item_data: ItemData):
	player_data.items.erase(item_data)
		

func create_new_item(item_config: ItemBaseConfig):
	var new_item_data = ItemData.new()
	new_item_data.item_config = item_config
	new_item_data.item_count = 1
	return new_item_data


func find_item_by_name(name: String) -> ItemData:
	var result = null
	
	for item in player_data.items:
		if item.item_config.name == name:
			result = item
			break
	
	return result
	

func get_items_by_type(types: Array[ItemBaseConfig.ItemType] ) -> Array[ItemData]:
	var result: Array[ItemData] = []
	for item_data in player_data.items:
		if item_data.item_config.item_type in types:
			result.append(item_data)
	return result
	
	
	
	
	
	
	
	
	
